package com.lin.linai.manager;

import com.lin.linai.common.ErrorCode;
import com.lin.linai.exception.BusinessException;
import com.zhipu.oapi.ClientV4;
import com.zhipu.oapi.Constants;
import com.zhipu.oapi.service.v4.model.ChatCompletionRequest;
import com.zhipu.oapi.service.v4.model.ChatMessage;
import com.zhipu.oapi.service.v4.model.ChatMessageRole;
import com.zhipu.oapi.service.v4.model.ModelApiResponse;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * 通用AI调用能力
 * @ClassName AiManager
 * @Description TODO
 * @Author ElijahG_O
 * @Date 2025-03-03 11:14
 */
public class AiManager {

    @Resource
    private ClientV4 clientV4;

    private static final float STABLE_TEMPERATURE = 0.05f;

    private static final float UNSTABLE_TEMPERATURE = 0.99f;


    //ChatMessage chatMessage = new ChatMessage(ChatMessageRole.USER.value(), "作为一名营销专家，请为智谱开放平台创作一个吸引人的slogan");
    //        messages.add(chatMessage);

    /**
     * 同步请求(答案不稳定)
     * @param systemMessage
     * @param userMessage
     * @return
     */
    public String doSyncUnstableRequest(String systemMessage, String userMessage){
        return doRequest(systemMessage,userMessage,Boolean.FALSE,UNSTABLE_TEMPERATURE);
    }

    /**
     * 同步请求(答案稳定)
     * @param systemMessage
     * @param userMessage
     * @return
     */
    public String doSyncStableRequest(String systemMessage, String userMessage){
        return doRequest(systemMessage,userMessage,Boolean.FALSE,STABLE_TEMPERATURE);
    }
    /**
     * 同步请求
     * @param systemMessage
     * @param userMessage
     * @param temperature
     * @return
     */
    public String doRequest(String systemMessage, String userMessage, Float temperature){
        return doRequest(systemMessage,userMessage,Boolean.FALSE,temperature);
    }
    /**
     * 通用请求方法（简化消息传递）
     * @param systemMessage
     * @param userMessage
     * @param stream
     * @param temperature
     * @return
     */
    public String doRequest(String systemMessage, String userMessage,Boolean stream, Float temperature){
        // 构造请求
        List<ChatMessage> chatMessageList = new ArrayList<>();
        ChatMessage systemChatMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), systemMessage);
        ChatMessage userChatMessage = new ChatMessage(ChatMessageRole.USER.value(), userMessage);
        chatMessageList.add(systemChatMessage);
        chatMessageList.add(userChatMessage);
        return doRequest(chatMessageList,stream,temperature);
    }


    /**
     * 通用请求方法
     * @param messages
     * @return
     */
    public String doRequest(List<ChatMessage> messages,Boolean stream, Float temperature){
        // 构造请求
        // 构造请求ID
//        String requestId = String.format(requestIdTemplate, System.currentTimeMillis());
        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                .model(Constants.ModelChatGLM4)
                .stream(stream)
                .temperature(temperature)
                .invokeMethod(Constants.invokeMethod)
                .messages(messages)
                .build();
        try {
            ModelApiResponse invokeModelApiResp = clientV4.invokeModelApi(chatCompletionRequest);
            return invokeModelApiResp.getData().getChoices().get(0).toString();
        } catch (Exception e) {
           e.printStackTrace();
           throw new BusinessException(ErrorCode.SYSTEM_ERROR, e.getMessage());
        }
    }
}
